<?php 
namespace app\controllers;

use Yii;
use yii\web\Controller;

class TextController extends Controller{
     public function actionChecktoken(){
     	if (!file_exists('token.txt')) {
     		file_put_contents('token.txt',json_encode(['access_token']=>'','expires_in'=>0,'add_time'=>0));
     	}
     	$token=file_get_contents("token.txt");
     	$toke_data=json_decode($token,true);
     	$expires_in=$toke_data['expires_in'];


     	if (empty($toke_data['add_time']) || ($toke_data['add_time']+$expires_in)<=time()) {
     		$appID="wxabf6d9a4d65ec549";
     		$appsecret="ceaf9421b684a85e560943881d21bf14";
     		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appID."&secret={$appsecret}";
     		$res=file_get_contents($url);
            $res=json_decode($res,true);
            $res['add_time']=time();
            file_put_contents("token.txt",json_encode($res));
     	}
     	$toke_data=file_get_contents("token.txt");
     	$toke_data=(json_decode($toke_data,true));
     	return $toke_data['access_token'];
     }
}